/**
 * Created by WYH on 17/1/3.
 */

var Service = (function(){
    var token = window.localStorage.token;
    console.log('token ' + token);

    //var CLIENT_API_HOST = "app-services.buluo-gs.com";
    var CLIENT_API_HOST = "123.56.251.131:10086";
    var CLIENT_BASE_URL = "http://" + CLIENT_API_HOST + "/tribalc/v1.0";
    var headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    };



    function getAjax(path, sucFuc, errFuc) {
        path = CLIENT_BASE_URL + "/" + path;
        console.log(headers);
        console.log(path);
        $.ajax({
            url: path,
            headers: headers,
            success: function (result) {
                sucFuc(result);
            },
            error: function (error) {
                errFuc(error);
            }
        });
    }

    function postAjax(path, data, sucFuc, errFuc, isNeedToken) {
        path = CLIENT_BASE_URL + "/" + path;
        console.log(path);
        console.log(data);
        var postHeaders = isNeedToken ? {
            "Content-Type": "application/json", "Accept": "application/json", "Authorization":token
        } : headers;
        console.log(postHeaders);
        $.ajax({
            url: path,
            method: "POST",
            headers: postHeaders,
            dataType: 'json',
            data: JSON.stringify(data),
            success: function(result) {
                sucFuc(result);
            },
            error: function(error) {
                errFuc(error);
            }
        })
    }

    function putAjax(path, data, sucFuc, errFuc, isNeedToken) {
        path = CLIENT_BASE_URL + "/" + path;
        console.log("path = " + path);
        console.log("data = " + data);

        var putHeaders = isNeedToken ? {
            "Content-Type": "application/json", "Accept": "application/json", "Authorization":token
        } : headers;
        console.log(putHeaders);
        ajaxData = {
            url: path,
            type: "PUT",
            headers: putHeaders,
            dataType: 'json',
            success: function(result) {
                sucFuc(result);
            },
            error: function(error) {
                errFuc(error);
            }
        };
        if (data != null) {
            ajaxData.data = JSON.stringify(data);
        }
        console.log(data);
        $.ajax(
            ajaxData
        );
    }

    function deleteAjax(path, sucFuc, errFuc, isNeedToken) {
        path = CLIENT_BASE_URL + "/" + path;
        console.log(headers);
        var deleteHeaders = isNeedToken ? {
            "Content-Type": "application/json", "Accept": "application/json", "Authorization":token
        } : headers;
        console.log(deleteHeaders);
        $.ajax({
            url: path,
            method: "DELETE",
            headers: deleteHeaders,
            dataType: 'json',
            success: function(result) {
                sucFuc(result);
            },
            error: function(error) {
                errFuc(error);
            }
        });
    }


    return {
        getAjax: getAjax,
        postAjax: postAjax,
        putAjax: putAjax,
        deleteAjax: deleteAjax,
        URL: CLIENT_BASE_URL
    }

})();
